package com.youu.youu.restapi.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youu.youu.manager.bean.TScenicdetails;
import com.youu.youu.restapi.bean.ScwReturn;
import com.youu.youu.restapi.service.ScenicDetailsService;


@RestController
@RequestMapping("/show/scenicdetail")
public class ScenicDetailsController {
	
    @Autowired
    ScenicDetailsService scenicDetailsService;
	
	@RequestMapping( value="/addScn", produces="html/text; charset=utf-8")
	public String addScn(MultipartFile file,HttpSession session){
		
		System.out.println("调用远程上传");
		
		
		if(file!=null){
		// 1、获取某个文件夹在服务器上的真实路径
        ServletContext context = session.getServletContext();
        // 2、使用context对象获取真实路径
        String scPath = context.getRealPath("/upload/scenic_img");
        System.out.println(scPath);
        // 3、把文件上传到这个位置
        String filename = UUID.randomUUID().toString().replace("-", "").substring(0, 10) + "_file_"
                + file.getOriginalFilename();
        // 文件上传后的网络位置
        try {
        	String netUrl = "/upload/scenic_img/" + filename;
        	//webPath不存在的情况下必须创建
        	File file2 = new File(scPath);
        	if(!file2.exists()){
        	//创建目录
        	file2.mkdirs();
        	}
        	file.transferTo(new File(scPath+"/"+filename));
        	//返回这个图片在服务器下的路径
        	return netUrl;
        	}catch (Exception e) {
        	return "上传失败";
        	}
		}
		return null;
	}
	
	@RequestMapping("/list")
    public ScwReturn<List<TScenicdetails>> toScenicsListPage(@RequestParam(value="select_key", defaultValue="null")String selectKey, @RequestParam(value="order", defaultValue="null")String order, @RequestParam(value="no",defaultValue="1")Integer pageNo) {
        
        TScenicdetails scenicdetails = new TScenicdetails();
        scenicdetails.setScenicName(selectKey);
        scenicdetails.setCity(selectKey);
        scenicdetails.setScenicLocation(selectKey);
        scenicdetails.setProvince(selectKey);
        
        List<TScenicdetails> scnList;
        
        if ("null".equals(selectKey)) {
//            if (order.equals("price")) PageHelper.startPage(pageNo, com.youu.youu.manager.constant.Constants.PAGE_SIZE, "ticket_price");
//            else PageHelper.startPage(pageNo, com.youu.youu.manager.constant.Constants.PAGE_SIZE);
            PageHelper.startPage(pageNo, com.youu.youu.manager.constant.Constants.PAGE_SIZE);
            scnList = scenicDetailsService.getList();
        } else {
//            if (order.equals("price")) PageHelper.startPage(pageNo, com.youu.youu.manager.constant.Constants.PAGE_SIZE, "ticket_price");
//            else PageHelper.startPage(pageNo, com.youu.youu.manager.constant.Constants.PAGE_SIZE);
          PageHelper.startPage(pageNo, com.youu.youu.manager.constant.Constants.PAGE_SIZE);
          scnList = scenicDetailsService.getScnListByBatch(scenicdetails);
        }
        
        if (scnList != null && scnList.size() > 0) return ScwReturn.success("成功！", scnList, null);
        return ScwReturn.fail("失败", null, null);
    }
}
